// 管理员
#include <string.h>
#include "admin.h"
#include "utils.h"

extern List *admin_list;

State admin_login(const char* account, const char * password) {
    if (account == NULL) {
        printf("账号不能为空\r\n");
        return Error;
    } else if (password == NULL) {
        printf("密码不能为空\r\n");
        return Error;
    }
    int res = strncmp(account, ADMIN_ACT, MAX_ADMIN_ACT);
    if (res < 0) {
        return NameError;
    }
    res = strncmp(password, ADMIN_PWD, MAX_ADMIN_PWD);
    if (res < 0) {
        return PassError;
    }
    return LoginOk;
}

int cmp_admin_name(const void* ptr1, const void* ptr2) {
    const Admin* admin = (Admin*)ptr1;
    return strncmp(admin->account, ptr2, MAX_ADMIN_ACT);
}

int cmp_admin_id(const void* ptr1, const void* ptr2) {
    const Admin* a1 = (Admin*)ptr1, *a2 = (Admin*)ptr2;
    if (a1->id > a2->id) {
        return 1;
    } else if (a1->id < a2->id) {
        return -1;
    } else {
        return 0;
    }
}

void show_admin(const void* ptr) {
    const Admin* admin = (Admin*)ptr;
    printf("编号：%d 账号：%s 性别：%c\r\n", admin->id, admin->account, admin->gender);
}

// 添加管理员
void admin_add() {
    Admin* user = malloc(ADMIN_SIZE);
    printf("添加用户：\r\n");
    printf("请输入账号：");
    get_str(user->account, 20);
    printf("请输入密码：");
    get_pwd(user->passwd, true, 20);
    printf("请输入性别：");
    user->gender = get_sex();
    printf("请输入编号：");
    scanf("%d", &user->id);
    getch();
    add_list_tail(admin_list, user);
}

// 删除管理员
void admin_del() {
    printf("删除用户：\r\n");
    printf("请输入用户名：");
    char account[20] = {};
    get_str(account, 20);

    bool res = delete_list_node_by_value(admin_list, account, cmp_admin_name);
    printf("删除用户：%s\r\n", res == true ? "成功":"失败");
}

// 修改管理员
void admin_chg() {
    printf("修改用户\r\n");
    printf("请输入编号：");

    char account[20] = {};
    int index = get_list_node_by_value(admin_list, account, cmp_admin_name);
    if (index < 0) {
        printf("查无此人\r\n");
        return;
    }
    Admin* admin = malloc(ADMIN_SIZE);
    printf("\r\n请输入密码：");
    get_pwd(admin->passwd, true, MAX_ADMIN_PWD);
    printf("\r\n请输入性别：");
    admin->gender = get_sex();
    printf("\r\n请输入编号：");
    scanf("%d", &admin->id);

    if (delete_list_node_by_index(admin_list, index)) {
        if (insert_list_node(admin_list, index, admin)) {
            printf("修改用户成功\r\n");
        } else {
            printf("修改用户失败\r\n");
        }
    } else {
        printf("删除用户失败\r\n");
    }
}

// 查找管理员
void admin_get() {
    printf("查询用户：\r\n");
    printf("请输入账号：");

    char account[20] = {};
    int index = get_list_node_by_value(admin_list, account, cmp_admin_name);
    if (index < 0) {
        printf("查无此人\r\n");
        return;
    } else {
        show_index(admin_list, index, show_admin);
    }
}

void admin_srt(void) {
    const Admin* arr[admin_list->size];
    int index = 0;
    quick_sort((void **)arr, index, cmp_admin_id);

    for(int i = 0; i < index; i++) {
        show_admin(arr[i]);
    }
}

void admin_shw(void) {
    printf("显示管理员\r\n");
    show_list(admin_list, show_admin);
    getch();
}
